package me.xiaoyan.android.tuition.activity;

import java.util.HashMap;
import java.util.Map;

import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.utils.AppManager;
import me.xiaoyan.android.tuition.utils.SESHttp;
import me.xiaoyan.android.tuition.utils.TuitionException;
import me.xiaoyan.android.widget.BaseUtil;
import me.xiaoyan.android.widget.DataBackListener;
import me.xiaoyan.android.widget.YLog;
import me.xiaoyan.json.JSONArray;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class StudentNoInput extends Common implements OnClickListener {
	private static final String TAG = "Activity.StudentNoInput";
	private String schoolId = "";
	private TextView txtStuNo = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stuno_input);
		txtStuNo = (TextView) findViewById(R.id.txt_stuno_inpput);
		setPageTitle(getString(R.string.header_stuno_input));
		Intent i = getIntent();
		schoolId = i.getStringExtra("schoolId");
		AppManager.getInstance().delAppDatas(STUDENTS_PAYS);
		findViewById(R.id.btn_stuno_input).setOnClickListener(this);
		super.showBackButton();
	}

	@Override
	public void onBackPressed() {
		super.goback();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_stuno_input) {
			String studentNo = txtStuNo.getText().toString();
			if (BaseUtil.stringIsEmpty(studentNo)) {
				showToast(getString(R.string.stuno_is_null), Toast.LENGTH_SHORT);
			} else {
				getStudentPays(studentNo);
			}
		}
	}

	private void getStudentPays(String studentNo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("schoolId", schoolId);
		param.put("studentNo", studentNo);
		param.put("pageIndex", 1);
		param.put("pageSize", 20);

		final ProgressDialog pd = getProgress(
				getString(R.string.loading_net_data), false);

		SESHttp.sendRequest(GET_STUDENT_PAYS, param, new DataBackListener() {
			@Override
			public void success(String source) {
				pd.dismiss();
				System.out.println("DataBackListener" + source);
				try {
					JSONArray dataArr = checkSESData(source);
					AppManager.getInstance().saveAppDatas(STUDENTS_PAYS,
							dataArr);
					startActivity(new Intent(StudentNoInput.this,
							PayItems.class));
				} catch (Exception e) {
					YLog.e(TAG, e);
					showToast(
							getString(R.string.load_data_failed)
									+ e.getMessage(), Toast.LENGTH_SHORT);
				}
			}

			@Override
			public void failed(TuitionException e) {
				pd.dismiss();
				YLog.e(TAG, e);
				showToast(
						getString(R.string.load_data_failed) + e.getMessage(),
						Toast.LENGTH_SHORT);
			}
		});
	}
}
